package com.naiterui.ehp.bs.esign.api.netrain.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bs.esign.utils.Constants;

/**
 * @author guoyongxiang
 * @date 2019-07-17 16:02
 * @since 1.0.0
 */
public class SealImageUtil {


    /**
     * 生成姓名签章
     *
     * @param text
     * @param path
     * @param color
     *
     * @throws IOException
     */
    public static boolean nameSealGeneration(String text, String path, Color color, String typeface) throws IOException, FontFormatException {
        int imageWidth = 150;
        int imageHeight = 100;
        double SCALE = 1;
        int w = (int) Math.round(imageWidth * SCALE);
        int h = (int) Math.round(imageHeight * SCALE);
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.scale(SCALE, SCALE);

        image = g2d.getDeviceConfiguration().createCompatibleImage(imageWidth, imageHeight, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();

        if (color == null) {
            color = Color.BLACK;
        }
        g2d.setColor(color);
        g2d.setStroke(new BasicStroke(3.0f));

        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);


        // 设置字体
        Font customFont;
        if (StringUtils.isBlank(typeface)){
            typeface = Constants.TYPEFACE_XINGKAI;
        }
        try (InputStream stream = SealImageUtil.class.getClassLoader().getResourceAsStream(typeface)) {
            customFont = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(Font.PLAIN, 50);
            GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
            environment.registerFont(customFont);
        }

        // 设置字体
        //Font font = new Font("楷体", Font.BOLD, 17);
        g2d.setFont(customFont);
        FontMetrics fm = g2d.getFontMetrics(customFont);

        int textWidth = fm.stringWidth(text);
        int textHeight = fm.getHeight();
        int textWidthX = (imageWidth - textWidth) / 2;

        g2d.drawString(text, textWidthX, textHeight);

        g2d.dispose();
        File file = new File(path);
        FileUtils.forceMkdirParent(file);
        return ImageIO.write(image, "PNG", file);
    }

    /**
     * 生成红框带标题+姓名签章
     * --------------------
     * |  海南版本互联网医院   |
     * |    医生:张三       |
     * --------------------
     *
     * @param title
     * @param text
     * @param path
     *
     * @throws IOException
     */
    public static void graphicsGeneration(String title, String text, String path) throws IOException {
        // 实际数据行数+标题+备注
        int imageWidth = 235;
        int imageHeight = 95;
        double SCALE = 1;
        int w = (int) Math.round(imageWidth * SCALE);
        int h = (int) Math.round(imageHeight * SCALE);
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.scale(SCALE, SCALE);


        // ---------- 增加下面的代码使得背景透明 -----------------
        //image = g2d.getDeviceConfiguration().createCompatibleImage(imageWidth, imageHeight, Transparency.TRANSLUCENT);
        //g2d.dispose();
        //g2d = image.createGraphics();
        // ---------- 背景透明代码结束 -----------------
        Color red = new Color(237, 123, 117);
        g2d.setColor(red);
        g2d.setStroke(new BasicStroke(3.0f));

        g2d.drawRect(5, 5, imageWidth - 5 * 2, imageHeight - 5 * 2);

        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);


        // 设置字体
        Font font = new Font("楷体\\-简", Font.BOLD, 17);
        g2d.setFont(font);
        FontMetrics fm = g2d.getFontMetrics(font);

        int textWidth = fm.stringWidth(text);
        int textWidthX = (imageWidth - textWidth) / 2;
        g2d.drawString(text, textWidthX, 33);

        int titleWidth = fm.stringWidth(title);
        int widthX = (imageWidth - titleWidth) / 2;
        g2d.drawString(title, widthX, 60);

        g2d.dispose();
        File file = new File(path);
        FileUtils.forceMkdirParent(file);
        ImageIO.write(image, "PNG", file);
    }

}
